<?php
/**
 * 商品管理
 *
 * 
 *
 *
 * @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */
defined('InShopNC') or exit ('Access Invalid!');
class store_goods_addControl extends BaseSellerControl {
    /**
     * 三方店铺验证，商品数量，有效期
     */
    private function checkStore(){
        if(!checkPlatformStore()){
            // 是否到达商品数上限
            $goods_num = Model('goods')->getGoodsCommonCount(array('store_id' => $_SESSION['store_id']));
            if (intval($this->store_grade['sg_goods_limit']) != 0) {
                if ($goods_num >= $this->store_grade['sg_goods_limit']) {
                    showMessage(L('store_goods_index_goods_limit') . $this->store_grade['sg_goods_limit'] . L('store_goods_index_goods_limit1'), 'index.php?act=store_goods&op=goods_list', 'html', 'error');
                }
            }
        }
    }
    public function __construct() {
        parent::__construct();
        Language::read('member_store_goods_index');
    }
    public function indexOp() {
        $this->checkStore();
        $this->add_step_oneOp();
    }

    /**
     * 获取规格模型
     * @return specModel
     */
    public function getModelSpec(){
        return Model('spec');
    }
    /**
     * 获取商品模型
     * @return goodsModel
     */
    public function getModelGoods(){
        return Model('goods');
    }
    /**
     * 导入网店管家数据
     * @return  void
     */
    public function importOp(){
        $nc=isset($_GET['nc_type'])?$_GET['nc_type']:'';
        switch($nc){
            case 'uploadedfile':
                if(isset($_FILES['csv'])&&!$_FILES['csv']['error']){
                    //读取文件
                    $fileData=file_get_contents($_FILES['csv']['tmp_name']);
                    //进行编码转换
                    if (!mb_check_encoding($fileData, 'utf-8')) {
                        $fileData=iconv('gbk', 'utf-8', $fileData);
                    }
                    $fileData=explode(PHP_EOL,trim($fileData));
                    $fields=str_getcsv(array_shift($fileData));
                    if(array_filter($fields)){
                        //自定价1 内部调货价
                        //自定价2 渠道价
                        Tpl::setLayout('null_layout');
                        $goods_fields=array(
                            'goods_serial'=>'货号',
                            'goods_name'=>'名称',
                            'goods_storage_unit.0'=>'规格',
                            'goods_storage_unit.1'=>'单位',
                            'goods_storage'=>'库存',
                            'goods_marketprice'=>'建议零售价',
                        );
                        //获取渠道价规格
                        $modelSpec=$this->getModelSpec();
                        $specList=$modelSpec->getGoodsChannel();
                        foreach($specList as $sp_id=>$item){
                            $goods_fields['goods_price.'.$sp_id]=$item['sp_value_name'].'-'.$item['member_level']['price_name'];
                        }
                        Tpl::output('goods_fields',$goods_fields);
                        Tpl::output('fields',$fields);
                        Tpl::output('data',$fileData);
                        Tpl::showpage('import.update');
                    }else{
                        echo '<script type="text/javascript">alert("数据格式不对或文件内容为空")</script>';
                    }
                }else{
                    echo '<script type="text/javascript">alert("数据格式不对或文件内容为空")</script>';
                }
                break;
            case 'import':
                set_time_limit(0);
                $result=array('status'=>true,'message'=>'','list'=>array());
                if(isset($_POST['list'])&&$_POST['list']){
                    $goodsList=json_decode(htmlspecialchars_decode($_POST['list']),true);
                    $model_goods=$this->getModelGoods();
                    $goods_serials=array_column($goodsList,'goods_serial');
                    $goods_serials=$model_goods->table('goods_common')->where(array('goods_serial'=>array('in',$goods_serials)))->field('goods_commonid,goods_serial')->limit(false)->select();
                    $goods_serials=$goods_serials?array_column($goods_serials,'goods_commonid','goods_serial'):array();
                    $goods_ids=array();
                    if($goods_serials){
                        $goods_ids=$model_goods->table('goods')->where(array('goods_commonid'=>array('in',array_values($goods_serials))))->field('goods_commonid,goods_channel_prices')->limit(false)->select();
                        $goods_ids=$goods_ids?array_column($goods_ids,'goods_channel_prices','goods_commonid'):array();
                    }
                    foreach($goodsList as $item){
                        $goods_serial=$item['goods_serial'];
                        $rowResult=array('has'=>false,'url'=>'');
                        //商品公共数据
                        $goodsCommon=array(
                            'goods_name'=>$item['goods_name'],//商品名称
                            'goods_serial'=>$goods_serial,//商家编号
                            'goods_marketprice'=>$item['goods_marketprice'],//建议零售价
                            'goods_costprice'=>$item['goods_marketprice'],//成本价
                            'goods_storage_unit' => $item['goods_storage_unit'][0]?: $item['goods_storage_unit'][1],//库存单位
                            'goods_vat' => 1,//是否开具增值税发票 1是，0否
                            'areaid_1' => 25,//一级地区id
                            'areaid_2' => 415,//二级地区id
                        );
                        if(!$goodsCommon['goods_storage_unit']){
                            $goodsCommon['goods_storage_unit']='1片/件';
                        }else if (mb_strlen($goodsCommon['goods_storage_unit'], 'utf-8') == 1) {
                            $goodsCommon['goods_storage_unit'] = '1' . $goodsCommon['goods_storage_unit'] . '/件';
                        }
                        if(strpos($goodsCommon['goods_storage_unit'],'*')!==false){
                            $goodsCommon['goods_storage_unit']=preg_replace('/\*\d+/','/',$goodsCommon['goods_storage_unit']);
                        }
                        $unit=sscanf($goodsCommon['goods_storage_unit'],'%d%[^/]/%s');
                        $goods_channel_prices=array();
                        foreach($item['goods_price'] as $channelId=>$price){
                            if(!isset($goodsCommon['goods_price'])){
                                $goodsCommon['goods_price']=$price*$unit[0];
                            }
                            $goods_channel_prices[$channelId]['price']=$price*$unit[0];
                        }
                        if(!isset($goodsCommon['goods_price'])){
                            $goodsCommon['goods_price']=$item['goods_marketprice'];
                        }
                        if(isset($goods_serials[$goods_serial])){
                            $goods_commonId=$goods_serials[$goods_serial];
                            //商品公共资料
                            $model_goods->table('goods_common')->where(array('goods_commonid'=>$goods_commonId))->update($goodsCommon);
                            $rowResult['has']=true;
                        }else{
                            //添加商品公共信息
                            if(substr($goodsCommon['goods_serial'], 0, 2) == 'sj'||strpos($goodsCommon['goods_name'],'书剑')!==false){
                                $goodsCommon['brand_id'] = 1;
                                $goodsCommon['brand_name'] = '书剑';
                                $goodsCommon['gc_id'] = 1074;//商品分类 shopnc_goods_class
                                $goodsCommon['gc_id_1'] = 1074;//一级分类id
                                $goodsCommon['gc_name'] = '书剑普洱茶';//商品分类
                            }else{
                                $goodsCommon['gc_id'] = 1075;//商品分类 shopnc_goods_class
                                $goodsCommon['gc_id_1'] = 1075;//一级分类id
                                $goodsCommon['gc_name'] = '内部产品';//商品分类
                                $goodsCommon['goods_price'] = $goodsCommon['goods_costprice'];//商品分类
                            }
                            $goodsCommon['goods_state']=0;//商品状态 0下架，1正常，10违规（禁售）
                            $goodsCommon['goods_verify']=1;//商品审核 1通过，0未通过，10审核中
                            $goodsCommon['goods_addtime']=time();//商品添加时间
                            $goodsCommon['goods_freight']= 0;//运费 0为免运费
                            $goodsCommon['goods_vat']= 1;//是否开具增值税发票 1是，0否
                            $goodsCommon['areaid_1']= 25;//一级地区id
                            $goodsCommon['areaid_2']= 415;//二级地区id
                            $goodsCommon['goods_stcids']= ',0,';//店铺分类id 首尾用,隔开
                            $goodsCommon['store_id'] = 1;//店铺id
                            $goodsCommon['store_name'] = '官方店铺';//店铺名称
                            $goodsCommon['gc_id_2'] = 0;//二级分类id
                            $goodsCommon['gc_id_3'] = 0;//三级分类id
                            $goodsCommon['goods_addtime'] = time();//商品添加时间
                            $goods_commonId=$model_goods->table('goods_common')->insert($goodsCommon);
                        }
                        $rowResult['url']=urlShop('store_goods_online','edit_goods',array('commonid'=>$goods_commonId));
                        $result['list'][$goods_serial]=$rowResult;
                        //处理商品数据
                        unset($goodsCommon['goods_storage_unit']);//库存单位
                        unset($goodsCommon['brand_name']);//品牌名称
                        unset($goodsCommon['type_id']);//类型id
                        unset($goodsCommon['goods_costprice']);//成本价
                        unset($goodsCommon['gc_name']);//商品分类
                        $goodsCommon['goods_storage']=$item['goods_storage'];//商品库存
                        if(isset($goods_ids[$goods_commonId])){
                            unset($goodsCommon['goods_addtime']);
                            $channel_prices=$goods_ids[$goods_commonId]?unserialize($goods_ids[$goods_commonId]):array();
                            foreach($channel_prices as $k=>$v){
                                $channel_prices[$k]['price']=isset($goods_channel_prices[$k])?$goods_channel_prices[$k]['price']:$v['price'];
                            }
                            $goodsCommon['goods_channel_prices']=serialize($channel_prices);
                            //更新数据
                            $model_goods->editGoods($goodsCommon,array('goods_commonid'=>$goods_commonId));
                        }else{
                            //添加数据
                            $goodsCommon['goods_commonid']=$goods_commonId;
                            $goodsCommon['goods_channel_prices']=serialize($goods_channel_prices);
                            $goodsCommon['goods_edittime']=time();//商品编辑时间
                            $model_goods->addGoods($goodsCommon);
                        }
                    }
                }else{
                    $result['status']=false;
                    $result['message']='没有数据';
                }
                echo json_encode($result,JSON_UNESCAPED_UNICODE);
                break;
            default:
                Tpl::showpage('import');
                break;
        }
    }
    /**
     * 添加商品
     */
    public function add_step_oneOp() {
        // 实例化商品分类模型
        $model_goodsclass = Model('goods_class');
        // 商品分类
        $goods_class = $model_goodsclass->getGoodsClass($_SESSION['store_id']);

        // 常用商品分类
        $model_staple = Model('goods_class_staple');
        $param_array = array();
        $param_array['member_id'] = $_SESSION['member_id'];
        $staple_array = $model_staple->getStapleList($param_array);
        
        Tpl::output('staple_array', $staple_array);
        Tpl::output('goods_class', $goods_class);
        Tpl::showpage('store_goods_add.step1');
    }
    
    /**
     * 添加商品
     */
    public function add_step_twoOp() {
        // 实例化商品分类模型
        $model_goodsclass = Model('goods_class');
        // 是否能使用编辑器
        if(checkPlatformStore()){ // 平台店铺可以使用编辑器
            $editor_multimedia = true;
        } else {    // 三方店铺需要
            $editor_multimedia = false;
            if ($this->store_grade['sg_function'] == 'editor_multimedia') {
                $editor_multimedia = true;
            }
        }
        Tpl::output('editor_multimedia', $editor_multimedia);
        
        $gc_id = intval($_GET['class_id']);
        
        // 验证商品分类是否存在且商品分类是否为最后一级
        $data = H('goods_class') ? H('goods_class') : H('goods_class', true);
        if (!isset($data[$gc_id]) || isset($data[$gc_id]['child']) || isset($data[$gc_id]['childchild'])) {
            showDialog(L('store_goods_index_again_choose_category1'));
        }
        
        // 三方店铺验证是否绑定了该分类
        if (!checkPlatformStore()) {
            $where['class_1|class_2|class_3'] = $gc_id;
            $where['store_id'] = $_SESSION['store_id'];
            $rs = Model('store_bind_class')->getStoreBindClassInfo($where);
            if (empty($rs)) {
                showMessage(L('store_goods_index_again_choose_category2'));
            }
        }
        
        // 更新常用分类信息
        $goods_class = $model_goodsclass->getGoodsClassLineForTag($gc_id);
        Tpl::output('goods_class', $goods_class);
        Model('goods_class_staple')->autoIncrementStaple($goods_class, $_SESSION['member_id']);
        
        // 获取类型相关数据
        if ($goods_class['type_id'] > 0) {
            $typeinfo = Model('type')->getAttr($goods_class['type_id'], $_SESSION['store_id'], $gc_id);
            list($spec_json, $spec_list, $attr_list, $brand_list) = $typeinfo;
            //增加渠道控制
            if(isset($spec_list[DEFAULT_SPEC_CHANNEL_ID])){
                $channelList=array('name'=>$spec_list[DEFAULT_SPEC_CHANNEL_ID]['sp_name']);
                foreach($spec_list[DEFAULT_SPEC_CHANNEL_ID]['value'] as $item){
                    $channelList['list'][$item['sp_value_id']]=array(
                        'name'=>$item['sp_value_name'],
                        'price'=>'0',
                        'limit'=>'0',
                        'member_level'=>$item['member_level_id'],
                    );
                }
                unset($spec_list[DEFAULT_SPEC_CHANNEL_ID]);
                Tpl::output('channel_list',$channelList);
            }
            Tpl::output('sign_i', count($spec_list));
            Tpl::output('spec_list', $spec_list);
            Tpl::output('attr_list', $attr_list);
            Tpl::output('brand_list', $brand_list);
        }

        // 实例化店铺商品分类模型
        $store_goods_class = Model('my_goods_class')->getClassTree(array(
                'store_id' => $_SESSION ['store_id'],
                'stc_state' => '1'
        ));
        Tpl::output('store_goods_class', $store_goods_class);
        
        // 小时分钟显示
        $hour_array = array('00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23');
        Tpl::output('hour_array', $hour_array);
        $minute_array = array('05', '10', '15', '20', '25', '30', '35', '40', '45', '50', '55');
        Tpl::output('minute_array', $minute_array);
        
        // 关联版式
        $plate_list = Model('store_plate')->getPlateList(array('store_id' => $_SESSION['store_id']), 'plate_id,plate_name,plate_position');
        $plate_list = array_under_reset($plate_list, 'plate_position', 2);
        Tpl::output('plate_list', $plate_list);
        
        Tpl::output('item_id', '');
        Tpl::output('menu_sign', 'add_goods_stpe2');
        Tpl::showpage('store_goods_add.step2');
    }

    /**
     * 保存商品（商品发布第二步使用）
     */
    public function save_goodsOp() {
        if (chksubmit()) {
            // 验证表单
            $obj_validate = new Validate();
            $obj_validate->validateparam = array(
                    array (
                            "input" => $_POST["g_name"],
                            "require" => "true",
                            "message" => L('store_goods_index_goods_name_null')
                    ),
                    array (
                            "input" => $_POST["g_price"],
                            "require" => "true",
                            "validator" => "Double",
                            "message" => L('store_goods_index_goods_price_null')
                    )
            );
            $error = $obj_validate->validate();
            if ($error != '') {
                showMessage(L('error') . $error, urlShop('seller_center'), 'html', 'error');
            }
            $model_goods = Model('goods');
            $model_type = Model('type');
            // 实例化商品分类模型
            $model_goodsclass = Model('goods_class');
            $goods_class = $model_goodsclass->getGoodsClassLineForTag(intval($_POST['cate_id']));
        
            $common_array = array();
            $common_array['goods_name']         = $_POST['g_name'];
            $common_array['goods_jingle']       = $_POST['g_jingle'];
            $common_array['gc_id']              = intval($_POST['cate_id']);
            $common_array['gc_id_1']            = $goods_class['gc_id_1'];//一级分类id
            $common_array['gc_id_2']            = $goods_class['gc_id_2']?:0;//二级分类id
            $common_array['gc_id_3']            = $goods_class['gc_id_3']?:0;//三级分类id
            $common_array['gc_name']            = $_POST['cate_name'];
            $common_array['brand_id']           = $_POST['b_id'];
            $common_array['brand_name']         = $_POST['b_name'];
            $common_array['type_id']            = intval($_POST['type_id']);
            $common_array['goods_image']        = $_POST['image_path'];
            $common_array['goods_price']        = floatval($_POST['g_price']);
            $common_array['goods_marketprice']  = floatval($_POST['g_marketprice']);
            $common_array['goods_costprice']    = floatval($_POST['g_costprice']);
            $common_array['goods_discount']     = floatval($_POST['g_discount']);
            $common_array['goods_serial']       = $_POST['g_serial'];
            //$common_array['goods_storage_alarm']= $_POST['g_alarm'];//库存预警值
            $common_array['goods_attr']         = serialize($_POST['attr']);
            $common_array['goods_body']         = $_POST['g_body'];
            $common_array['goods_commend']      = intval($_POST['g_commend']);
            $common_array['goods_state']        = ($this->store_info['store_state'] != 1) ? 0 : intval($_POST['g_state']);            // 店铺关闭时，商品下架
            $common_array['goods_addtime']      = TIMESTAMP;
            $common_array['goods_selltime']     = strtotime($_POST['starttime']) + intval($_POST['starttime_H'])*3600 + intval($_POST['starttime_i'])*60;
            $common_array['goods_verify']       = (C('goods_verify') == 1) ? 10 : 1;
            $common_array['store_id']           = $_SESSION['store_id'];
            $common_array['store_name']         = $_SESSION['store_name'];
            $common_array['spec_name']          = is_array($_POST['spec']) ? serialize($_POST['sp_name']) : serialize(null);
            $common_array['spec_value']         = is_array($_POST['spec']) ? serialize($_POST['sp_val']) : serialize(null);
            $common_array['goods_vat']          = intval($_POST['g_vat']);
            $common_array['areaid_1']           = intval($_POST['province_id']);
            $common_array['areaid_2']           = intval($_POST['city_id']);
            $common_array['transport_id']       = ($_POST['freight'] == '0') ? '0' : intval($_POST['transport_id']); // 运费模板
            $common_array['transport_title']    = $_POST['transport_title'];
            $common_array['goods_freight']      = floatval($_POST['g_freight']);
            $common_array['goods_stcids']       = ',' . implode(',', array_unique($_POST['sgcate_id'])) . ',';    // 首尾需要加,
            $common_array['plateid_top']        = intval($_POST['plate_top']) > 0 ? intval($_POST['plate_top']) : '';
            $common_array['plateid_bottom']     = intval($_POST['plate_bottom']) > 0 ? intval($_POST['plate_bottom']) : '';
            $common_array['is_gv']              = intval($_POST['is_gv']);
            //新增库存单位
            $common_array['goods_storage_unit']  = trim($_POST['goods_storage_unit']);
			if($_POST['is_gv']==0)
			{
			$g_vindate='0';
			$g_vlimit='0';
			$g_vinvalidrefund='0';

			}
			else
			{
			$g_vindate=strtotime($_POST['g_vindate']);
			$g_vlimit=intval($_POST['g_vlimit']);
			$g_vinvalidrefund=intval($_POST['g_vinvalidrefund']);
			}
            $common_array['g_vindate']         = $g_vindate;
            $common_array['g_vlimit']          = $g_vlimit;
            $common_array['g_vinvalidrefund']  = $g_vinvalidrefund;
            // 保存数据
            $common_id = $model_goods->addGoods($common_array, 'goods_common');
            //处理渠道价
            $channel_price=array();
            if(isset($_POST['channel'])){
                $channel_price= array_map(function(&$item){
                    settype($item['price'],'float');
                    settype($item['limit'],'float');
                    return $item;
                },$_POST['channel']);
            }
            $channel_price=serialize($channel_price);

            if ($common_id) {
                // 生成商品二维码
                require_once(BASE_RESOURCE_PATH.DS.'phpqrcode'.DS.'index.php');
                $PhpQRCode = new PhpQRCode();
                $PhpQRCode->set('pngTempDir',BASE_UPLOAD_PATH.DS.ATTACH_STORE.DS.$_SESSION['store_id'].DS);
                // 商品规格
                if (is_array($_POST['spec'])) {
                    foreach ($_POST['spec'] as $value) {
                        $goods = array();
                        $goods['goods_commonid']    = $common_id;
                        $goods['goods_name']        = $common_array['goods_name'];
                        $goods['goods_jingle']      = $common_array['goods_jingle'];
                        $goods['store_id']          = $common_array['store_id'];
                        $goods['store_name']        = $_SESSION['store_name'];
                        $goods['gc_id']             = $common_array['gc_id'];
                        $goods['gc_id_1']           = $common_array['gc_id_1'];//一级分类id
                        $goods['gc_id_2']           = $common_array['gc_id_2'];//二级分类id
                        $goods['gc_id_3']           = $common_array['gc_id_3'];//三级分类id
                        $goods['brand_id']          = $common_array['brand_id'];
                        $goods['goods_price']       = $value['price'];
                        $goods['goods_marketprice'] = $common_array['goods_marketprice'];
                        $goods['goods_serial']      = $value['sku'];
                        //$goods['goods_storage_alarm']= $common_array['goods_storage_alarm'];//库存预警值
                        $goods['goods_spec']        = serialize($value['sp_value']);
                        $goods['goods_storage']     = floatval($value['stock']);
                        $goods['goods_image']       = $common_array['goods_image'];
                        $goods['goods_state']       = $common_array['goods_state'];
                        $goods['goods_verify']      = $common_array['goods_verify'];
                        $goods['goods_addtime']     = TIMESTAMP;
                        $goods['goods_edittime']    = TIMESTAMP;
                        $goods['areaid_1']          = $common_array['areaid_1'];
                        $goods['areaid_2']          = $common_array['areaid_2'];
                        $goods['color_id']          = intval($value['color']);
                        $goods['transport_id']      = $common_array['transport_id'];
                        $goods['goods_freight']     = $common_array['goods_freight'];
                        $goods['goods_vat']         = $common_array['goods_vat'];
                        $goods['goods_commend']     = $common_array['goods_commend'];
                        $goods['goods_stcids']      = $common_array['goods_stcids'];
                        $goods['is_gv']              = intval($_POST['is_gv']);
						
                        $goods['g_vindate']          = intval($_POST['g_vindate']) > 0 ? strtotime($_POST['g_vindate']) : '';
                        $goods['g_vlimit']           = intval($_POST['g_vlimit']);
                        $goods['g_vinvalidrefund']  = intval($_POST['g_vinvalidrefund']);
                        //增加商品渠道价
                        $goods['goods_channel_prices']=$channel_price;
                        //虚拟库存
                        $goods['goods_storage_virtual']=floatval($_POST['goods_storage_virtual']);
                        //是否用文字表示库存
                        $goods['goods_storage_show']=intval($_POST['goods_storage_show']);

                        $goods_id = $model_goods->addGoods($goods);
                        $model_type->addGoodsType($goods_id, $common_id, array('cate_id' => $_POST['cate_id'], 'type_id' => $_POST['type_id'], 'attr' => $_POST['attr']));

                        // 生成商品二维码
                        $PhpQRCode->set('date',WAP_SITE_URL . '/tmpl/product_detail.html?goods_id='.$goods_id);
                        $PhpQRCode->set('pngTempName', $goods_id . '.png');
                        $PhpQRCode->init();
                    }
                } else {
                    $goods = array();
                    $goods['goods_commonid']    = $common_id;
                    $goods['goods_name']        = $common_array['goods_name'];
                    $goods['goods_jingle']      = $common_array['goods_jingle'];
                    $goods['store_id']          = $common_array['store_id'];
                    $goods['store_name']        = $_SESSION['store_name'];
                    $goods['gc_id']             = $common_array['gc_id'];
                    $goods['gc_id_1']           = $common_array['gc_id_1'];//一级分类id
                    $goods['gc_id_2']           = $common_array['gc_id_2'];//二级分类id
                    $goods['gc_id_3']           = $common_array['gc_id_3'];//三级分类id
                    $goods['brand_id']          = $common_array['brand_id'];
                    $goods['goods_price']       = $common_array['goods_price'];
                    $goods['goods_marketprice'] = $common_array['goods_marketprice'];
                    $goods['goods_serial']      = $common_array['goods_serial'];
                    //$goods['goods_storage_alarm']= $common_array['goods_storage_alarm'];//库存预警值
                    $goods['goods_spec']        = serialize(null);
                    $goods['goods_storage']     = floatval($_POST['g_storage']);
                    $goods['goods_image']       = $common_array['goods_image'];
                    $goods['goods_state']       = $common_array['goods_state'];
                    $goods['goods_verify']      = $common_array['goods_verify'];
                    $goods['goods_addtime']     = TIMESTAMP;
                    $goods['goods_edittime']    = TIMESTAMP;
                    $goods['areaid_1']          = $common_array['areaid_1'];
                    $goods['areaid_2']          = $common_array['areaid_2'];
                    $goods['color_id']          = 0;
                    $goods['transport_id']      = $common_array['transport_id'];
                    $goods['goods_freight']     = $common_array['goods_freight'];
                    $goods['goods_vat']         = $common_array['goods_vat'];
                    $goods['goods_commend']     = $common_array['goods_commend'];
                    $goods['goods_stcids']      = $common_array['goods_stcids'];
                    $goods['is_gv']              = intval($_POST['is_gv']);
                    $goods['g_vindate']          = intval($_POST['g_vindate']) > 0 ? strtotime($_POST['g_vindate']) : '';
                    $goods['g_vlimit']           = intval($_POST['g_vlimit']);
                    $goods['g_vinvalidrefund']  = intval($_POST['g_vinvalidrefund']);
                    //增加商品渠道价
                    $goods['goods_channel_prices']=$channel_price;
                    //虚拟库存
                    $goods['goods_storage_virtual']=floatval($_POST['goods_storage_virtual']);
                    //是否用文字表示库存
                    $goods['goods_storage_show']=intval($_POST['goods_storage_show']);
                    $goods_id = $model_goods->addGoods($goods);
                    $model_type->addGoodsType($goods_id, $common_id, array('cate_id' => $_POST['cate_id'], 'type_id' => $_POST['type_id'], 'attr' => $_POST['attr']));

                    // 生成商品二维码
                    $PhpQRCode->set('date',WAP_SITE_URL . '/tmpl/product_detail.html?goods_id=' . $goods_id);
                    $PhpQRCode->set('pngTempName', $goods_id . '.png');
                    $PhpQRCode->init();
                }

                // 商品加入上架队列
                if (isset($_POST['starttime'])) {
                    $selltime = strtotime($_POST['starttime']) + intval($_POST['starttime_H'])*3600 + intval($_POST['starttime_i'])*60;
                    if ($selltime > TIMESTAMP) {
                        $this->addcron(array('exetime' => $selltime, 'exeid' => $common_id, 'type' => 1));
                    }
                }
                // 记录日志
                $this->recordSellerLog('添加商品，平台货号:'.$common_id);
                redirect(urlShop('store_goods_add', 'add_step_three', array('commonid' => $common_id)));
            } else {
                showMessage(L('store_goods_index_goods_add_fail'), getReferer(), 'html', 'error');
            }
        }
    }

    /**
     * 第三步添加颜色图片
     */
    public function add_step_threeOp() {
        $common_id = intval($_GET['commonid']);
        if ($common_id <= 0) {
            showMessage(L('wrong_argument'), urlShop('seller_center'), 'html', 'error');
        }
        
        $model_goods = Model('goods');
        $img_array = $model_goods->getGoodsList(array('goods_commonid' => $common_id), 'color_id,goods_image', 'color_id');
        // 整理，更具id查询颜色名称
        if (!empty($img_array)) {
            $colorid_array = array();
            $image_array = array();
            foreach ($img_array as $val) {
                $image_array[$val['color_id']][0]['goods_image'] = $val['goods_image'];
                $image_array[$val['color_id']][0]['is_default'] = 1;
                $colorid_array[] = $val['color_id'];
            }
            Tpl::output('img', $image_array);
        }
        
        $common_list = $model_goods->getGoodeCommonInfo(array('goods_commonid' => $common_id), 'spec_value');
        $spec_value = unserialize($common_list['spec_value']);
        Tpl::output('value', $spec_value['1']);
        
        $model_spec = Model('spec');
        $value_array = $model_spec->getSpecValueList(array('sp_value_id' => array('in', $colorid_array), 'store_id' => $_SESSION['store_id']), 'sp_value_id,sp_value_name');
        if (empty($value_array)) {
            $value_array[] = array('sp_value_id' => '0', 'sp_value_name' => '无颜色');
        }
        Tpl::output('value_array', $value_array);
        
        Tpl::output('commonid', $common_id);
        Tpl::showpage('store_goods_add.step3');
    }
    
    /**
     * 保存商品颜色图片
     */
    public function save_imageOp(){
        if (chksubmit()) {
            $common_id = intval($_POST['commonid']);
            if ($common_id <= 0 || empty($_POST['img'])) {
                showMessage(L('wrong_argument'));
            }
            $model_goods = Model('goods');
            // 保存
            $insert_array = array();
            foreach ($_POST['img'] as $key => $value) {
                foreach ($value as $k => $v) {
                    // 商品默认主图
                    $update_array = array();        // 更新商品主图
                    $update_where = array();
                    if ($k == 0 || $v['default'] == 1) {
                        $update_array['goods_image']    = $v['name'];
                        $update_where['goods_commonid'] = $common_id;
                        $update_where['color_id']       = $key;
                        // 更新商品主图
                        $model_goods->editGoods($update_array, $update_where);
                    }
                    if ($v['name'] == '') {
                        continue;
                    }
                    $tmp_insert = array();
                    $tmp_insert['goods_commonid']   = $common_id;
                    $tmp_insert['store_id']         = $_SESSION['store_id'];
                    $tmp_insert['color_id']         = $key;
                    $tmp_insert['goods_image']      = $v['name'];
                    $tmp_insert['goods_image_sort'] = ($v['default'] == 1) ? 0 : intval($v['sort']);
                    $tmp_insert['is_default']       = $v['default'];
                    $insert_array[] = $tmp_insert;
                }
            }
            $rs = $model_goods->addGoodsAll($insert_array, 'goods_images');
            if ($rs) {
                redirect(urlShop('store_goods_add', 'add_step_four', array('commonid' => $common_id)));
            } else {
                showMessage(L('nc_common_save_fail'));
            }
        }
    }
    
    /**
     * 商品发布第四步
     */
    public function add_step_fourOp() {
        // 单条商品信息
        $goods_info = Model('goods')->getGoodsInfo(array('goods_commonid' => $_GET['commonid']));

        // 自动发布动态
        $data_array = array();
        $data_array['goods_id'] = $goods_info['goods_id'];
        $data_array['store_id'] = $goods_info['store_id'];
        $data_array['goods_name'] = $goods_info['goods_name'];
        $data_array['goods_image'] = $goods_info['goods_image'];
        $data_array['goods_price'] = $goods_info['goods_price'];
        $data_array['goods_transfee_charge'] = $goods_info['goods_freight'] == 0 ? 1 : 0;
        $data_array['goods_freight'] = $goods_info['goods_freight'];
        $this->storeAutoShare($data_array, 'new');

        Tpl::output('goods_id', $goods_info['goods_id']);
        Tpl::showpage('store_goods_add.step4');
    }
    
    /**
     * 上传图片
     */
    public function image_uploadOp() {
        // 判断图片数量是否超限
        $model_album = Model('album');
        $album_limit = $this->store_grade['sg_album_limit'];
        $album_count = $model_album->getCount(array('store_id' => $_SESSION['store_id']));
        if ($album_count >= $album_limit) {
            $error = L('store_goods_album_climit');
            if (strtoupper(CHARSET) == 'GBK') {
                $error = Language::getUTF8($error);
            }
            exit(json_encode(array('error' => $error)));
        }
        $class_info = $model_album->getOne(array('store_id' => $_SESSION['store_id'], 'is_default' => 1), 'album_class');
        // 上传图片
        $upload = new UploadFile();
        $upload->set('default_dir', ATTACH_GOODS . DS . $_SESSION ['store_id'] . DS . $upload->getSysSetPath());
        $upload->set('max_size', C('image_max_filesize'));

        $upload->set('thumb_width', GOODS_IMAGES_WIDTH);
        $upload->set('thumb_height', GOODS_IMAGES_HEIGHT);
        $upload->set('thumb_ext', GOODS_IMAGES_EXT);
        $upload->set('fprefix', $_SESSION['store_id']);
        $upload->set('allow_type', array('gif', 'jpg', 'jpeg', 'png'));
        $result = $upload->upfile($_POST['name']);
        if (!$result) {
            if (strtoupper(CHARSET) == 'GBK') {
                $upload->error = Language::getUTF8($upload->error);
            }
            $output = array();
            $output['error'] = $upload->error;
            $output = json_encode($output);
            exit($output);
        }
        
        $img_path = $upload->getSysSetPath() . $upload->file_name;
        
        // 取得图像大小
        list($width, $height, $type, $attr) = getimagesize(UPLOAD_SITE_URL . '/' . ATTACH_GOODS . '/' . $_SESSION['store_id'] . DS . $img_path);
        
        // 存入相册
        $image = explode('.', $_FILES[$_POST['name']]["name"]);
        $insert_array = array();
        $insert_array['apic_name'] = $image['0'];
        $insert_array['apic_tag'] = '';
        $insert_array['aclass_id'] = $class_info['aclass_id'];
        $insert_array['apic_cover'] = $img_path;
        $insert_array['apic_size'] = intval($_FILES[$_POST['name']]['size']);
        $insert_array['apic_spec'] = $width . 'x' . $height;
        $insert_array['upload_time'] = TIMESTAMP;
        $insert_array['store_id'] = $_SESSION['store_id'];
        $model_album->addPic($insert_array);
        
        $data = array ();
        $data ['thumb_name'] = cthumb($upload->getSysSetPath() . $upload->thumb_image, 240, $_SESSION['store_id']);
        $data ['name']      = $img_path;

        // 整理为json格式
        $output = json_encode($data);
        echo $output;
        exit();
    }

    /**
     * ajax获取商品分类的子级数据
     */
    public function ajax_goods_classOp() {
        $gc_id = intval($_GET['gc_id']);
        $deep = intval($_GET['deep']);
        if ($gc_id <= 0 || $deep <= 0 || $deep >= 4) {
            exit();
        }
        $model_goodsclass = Model('goods_class');
        $list = $model_goodsclass->getGoodsClass($_SESSION['store_id'], $gc_id, $deep);
        if (empty($list)) {
            exit();
        }
        /**
         * 转码
         */
        if (strtoupper ( CHARSET ) == 'GBK') {
            $list = Language::getUTF8 ( $list );
        }
        echo json_encode($list);
    }
    /**
     * ajax删除常用分类
     */
    public function ajax_stapledelOp() {
        Language::read ( 'member_store_goods_index' );
        $staple_id = intval($_GET ['staple_id']);
        if ($staple_id < 1) {
            echo json_encode ( array (
                    'done' => false,
                    'msg' => Language::get ( 'wrong_argument' ) 
            ) );
            die ();
        }
        /**
         * 实例化模型
         */
        $model_staple = Model('goods_class_staple');

        $result = $model_staple->delStaple(array('staple_id' => $staple_id, 'member_id' => $_SESSION['member_id']));
        if ($result) {
            echo json_encode ( array (
                    'done' => true 
            ) );
            die ();
        } else {
            echo json_encode ( array (
                    'done' => false,
                    'msg' => '' 
            ) );
            die ();
        }
    }
    /**
     * ajax选择常用商品分类
     */
    public function ajax_show_commOp() {
        $staple_id = intval($_GET['stapleid']);
        
        /**
         * 查询相应的商品分类id
         */
        $model_staple = Model('goods_class_staple');
        $staple_info = $model_staple->getStapleInfo(array('staple_id' => intval($staple_id), 'gc_id_1,gc_id_2,gc_id_3'));
        if (empty ( $staple_info ) || ! is_array ( $staple_info )) {
            echo json_encode ( array (
                    'done' => false,
                    'msg' => '' 
            ) );
            die ();
        }
        
        $list_array = array ();
        $list_array['gc_id'] = 0;
        $list_array['type_id'] = $staple_info['type_id'];
        $list_array['done'] = true;
        $list_array['one'] = '';
        $list_array['two'] = '';
        $list_array['three'] = '';
        
        $gc_id_1 = intval ( $staple_info['gc_id_1'] );
        $gc_id_2 = intval ( $staple_info['gc_id_2'] );
        $gc_id_3 = intval ( $staple_info['gc_id_3'] );
        
        /**
         * 查询同级分类列表
         */
        $model_goods_class = Model ( 'goods_class' );
        // 1级
        if ($gc_id_1 > 0) {
            $list_array['gc_id'] = $gc_id_1;
            $class_list = $model_goods_class->getGoodsClass($_SESSION['store_id']);
            if (empty ( $class_list ) || ! is_array ( $class_list )) {
                echo json_encode ( array (
                        'done' => false,
                        'msg' => '' 
                ) );
                die ();
            }
            foreach ( $class_list as $val ) {
                if ($val ['gc_id'] == $gc_id_1) {
                    $list_array ['one'] .= '<li class="" onclick="selClass($(this));" data-param="{gcid:' . $val ['gc_id'] . ', deep:1, tid:' . $val ['type_id'] . '}" nctype="selClass"> <a class="classDivClick" href="javascript:void(0)"><span class="has_leaf"><i class="icon-double-angle-right"></i>' . $val ['gc_name'] . '</span></a> </li>';
                } else {
                    $list_array ['one'] .= '<li class="" onclick="selClass($(this));" data-param="{gcid:' . $val ['gc_id'] . ', deep:1, tid:' . $val ['type_id'] . '}" nctype="selClass"> <a class="" href="javascript:void(0)"><span class="has_leaf"><i class="icon-double-angle-right"></i>' . $val ['gc_name'] . '</span></a> </li>';
                }
            }
        }
        // 2级
        if ($gc_id_2 > 0) {
            $list_array['gc_id'] = $gc_id_2;
            $class_list = $model_goods_class->getGoodsClass($_SESSION['store_id'], $gc_id_1, 2);
            if (empty ( $class_list ) || ! is_array ( $class_list )) {
                echo json_encode ( array (
                        'done' => false,
                        'msg' => '' 
                ) );
                die ();
            }
            foreach ( $class_list as $val ) {
                if ($val ['gc_id'] == $gc_id_2) {
                    $list_array ['two'] .= '<li class="" onclick="selClass($(this));" data-param="{gcid:' . $val ['gc_id'] . ', deep:2, tid:' . $val ['type_id'] . '}" nctype="selClass"> <a class="classDivClick" href="javascript:void(0)"><span class="has_leaf"><i class="icon-double-angle-right"></i>' . $val ['gc_name'] . '</span></a> </li>';
                } else {
                    $list_array ['two'] .= '<li class="" onclick="selClass($(this));" data-param="{gcid:' . $val ['gc_id'] . ', deep:2, tid:' . $val ['type_id'] . '}" nctype="selClass"> <a class="" href="javascript:void(0)"><span class="has_leaf"><i class="icon-double-angle-right"></i>' . $val ['gc_name'] . '</span></a> </li>';
                }
            }
        }
        // 3级
        if ($gc_id_3 > 0) {
            $list_array['gc_id'] = $gc_id_3;
            $class_list = $model_goods_class->getGoodsClass($_SESSION['store_id'], $gc_id_2, 3);
            if (empty ( $class_list ) || ! is_array ( $class_list )) {
                echo json_encode ( array (
                        'done' => false,
                        'msg' => '' 
                ) );
                die ();
            }
            foreach ( $class_list as $val ) {
                if ($val ['gc_id'] == $gc_id_3) {
                    $list_array ['three'] .= '<li class="" onclick="selClass($(this));" data-param="{gcid:' . $val ['gc_id'] . ', deep:3, tid:' . $val ['type_id'] . '}" nctype="selClass"> <a class="classDivClick" href="javascript:void(0)"><span class="has_leaf"><i class="icon-double-angle-right"></i>' . $val ['gc_name'] . '</span></a> </li>';
                } else {
                    $list_array ['three'] .= '<li class="" onclick="selClass($(this));" data-param="{gcid:' . $val ['gc_id'] . ', deep:3, tid:' . $val ['type_id'] . '}" nctype="selClass"> <a class="" href="javascript:void(0)"><span class="has_leaf"><i class="icon-double-angle-right"></i>' . $val ['gc_name'] . '</span></a> </li>';
                }
            }
        }
        // 转码
        if (strtoupper ( CHARSET ) == 'GBK') {
            $list_array = Language::getUTF8 ( $list_array );
        }
        echo json_encode ( $list_array );
        die ();
    }
    /**
     * AJAX添加商品规格值
     */
    public function ajax_add_specOp() {
        $name = trim($_GET['name']);
        $gc_id = intval($_GET['gc_id']);
        $sp_id = intval($_GET['sp_id']);
        if ($name == '' || $gc_id <= 0 || $sp_id <= 0) {
            echo json_encode(array('done' => false));die();
        }
        $insert = array(
            'sp_value_name' => $name,
            'sp_id' => $sp_id,
            'gc_id' => $gc_id,
            'store_id' => $_SESSION['store_id'],
            'sp_value_color' => null,
            'sp_value_sort' => 0,
        );
        $value_id = Model('spec')->addSpecValue($insert);
        if ($value_id) {
            echo json_encode(array('done' => true, 'value_id' => $value_id));die();
        } else {
            echo json_encode(array('done' => false));die();
        }
    }
}
